home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 May / maximum-cd-2009-05.iso / DiscContents / Firefox Setup 3.0.6.exe / nonlocalized / chrome / toolkit.jar / content / mozapps / plugins / pluginInstallerService.js < prev    next >
Encoding:
Text File  |  2007-07-25  |  9.3 KB  |  281 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Plugin Finder Service.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * IBM Corporation.
  18.  * Portions created by the IBM Corporation are Copyright (C) 2004
  19.  * IBM Corporation. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *   Doron Rosenberg <doronr@us.ibm.com>
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  26.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. const nsIXPIProgressDialog = Components.interfaces.nsIXPIProgressDialog;
  39.  
  40. function getLocalizedError(key)
  41. {
  42.   return document.getElementById("xpinstallStrings").getString(key);
  43. }
  44.  
  45. function binaryToHex(input)
  46. {
  47.   return [('0' + input.charCodeAt(i).toString(16)).slice(-2)
  48.           for (i in input)].join('');
  49. }
  50.  
  51. function verifyHash(aFile, aHash)
  52. {
  53.   try {
  54.     var [, method, hash] = /^([A-Za-z0-9]+):(.*)$/.exec(aHash);
  55.  
  56.     var fis = Components.classes['@mozilla.org/network/file-input-stream;1'].
  57.       createInstance(Components.interfaces.nsIFileInputStream);
  58.     fis.init(aFile, -1, -1, 0);
  59.  
  60.     var hasher = Components.classes['@mozilla.org/security/hash;1'].
  61.       createInstance(Components.interfaces.nsICryptoHash);
  62.     hasher.initWithString(method);
  63.     hasher.updateFromStream(fis, -1);
  64.     dlhash = binaryToHex(hasher.finish(false));
  65.     return dlhash == hash;
  66.   }
  67.   catch (e) {
  68.     Components.utils.reportError(e);
  69.     return false;
  70.   }
  71. }
  72.  
  73. function InstallerObserver(aPlugin)
  74. {
  75.   this._plugin = aPlugin;
  76.   this._init();
  77. }
  78.  
  79. InstallerObserver.prototype = {
  80.   _init: function()
  81.   {
  82.     try {
  83.       var ios = Components.classes["@mozilla.org/network/io-service;1"].
  84.         getService(Components.interfaces.nsIIOService);
  85.       var uri = ios.newURI(this._plugin.InstallerLocation, null, null);
  86.       uri.QueryInterface(Components.interfaces.nsIURL);
  87.  
  88.       var leafName = uri.fileName;
  89.       if (leafName.indexOf('.') == -1)
  90.         throw "Filename needs to contain a dot for platform-native launching to work correctly.";
  91.  
  92.       var dirs = Components.classes["@mozilla.org/file/directory_service;1"].
  93.         getService(Components.interfaces.nsIProperties);
  94.  
  95.       var resultFile = dirs.get("TmpD", Components.interfaces.nsIFile);
  96.       resultFile.append(leafName);
  97.       resultFile.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE,
  98.                               0x770);
  99.  
  100.       var channel = ios.newChannelFromURI(uri);
  101.       this._downloader =
  102.         Components.classes["@mozilla.org/network/downloader;1"].
  103.           createInstance(Components.interfaces.nsIDownloader);
  104.       this._downloader.init(this, resultFile);
  105.       channel.notificationCallbacks = this;
  106.  
  107.       this._fireNotification(nsIXPIProgressDialog.DOWNLOAD_START, null);
  108.  
  109.       channel.asyncOpen(this._downloader, null);
  110.     }
  111.     catch (e) {
  112.       this._fireNotification(nsIXPIProgressDialog.INSTALL_DONE,
  113.                              getLocalizedError("error-228"));
  114.       if (resultFile && resultFile.exists())
  115.         resultfile.remove(false);
  116.     }
  117.   },
  118.  
  119.   /**
  120.    * Inform the gPluginInstaller about what's going on.
  121.    */
  122.   _fireNotification: function(aStatus, aErrorMsg)
  123.   {
  124.     gPluginInstaller.pluginInstallationProgress(this._plugin.pid,
  125.                                                 aStatus, aErrorMsg);
  126.  
  127.     if (aStatus == nsIXPIProgressDialog.INSTALL_DONE) {
  128.       --PluginInstallService._installersPending;
  129.       PluginInstallService._fireFinishedNotification();
  130.     }
  131.   },
  132.  
  133.   QueryInterface: function(iid)
  134.   {
  135.     if (iid.equals(Components.interfaces.nsISupports) ||
  136.         iid.equals(Components.interfaces.nsIInterfaceRequestor) ||
  137.         iid.equals(Components.interfaces.nsIDownloadObserver) ||
  138.         iid.equals(Components.interfaces.nsIProgressEventSink))
  139.       return this;
  140.  
  141.     throw Components.results.NS_ERROR_NO_INTERFACE;
  142.   },
  143.  
  144.   getInterface: function(iid)
  145.   {
  146.     if (iid.equals(Components.interfaces.nsIProgressEventSink))
  147.       return this;
  148.  
  149.     return null;
  150.   },
  151.  
  152.   onDownloadComplete: function(downloader, request, ctxt, status, result)
  153.   {
  154.     if (!Components.isSuccessCode(status)) {
  155.       // xpinstall error 228 is "Download Error"
  156.       this._fireNotification(nsIXPIProgressDialog.INSTALL_DONE,
  157.                              getLocalizedError("error-228"));
  158.       result.remove(false);
  159.       return;
  160.     }
  161.  
  162.     this._fireNotification(nsIXPIProgressDialog.DOWNLOAD_DONE);
  163.  
  164.     if (this._plugin.InstallerHash &&
  165.         !verifyHash(result, this._plugin.InstallerHash)) {
  166.       // xpinstall error 261 is "Invalid file hash..."
  167.       this._fireNotification(nsIXPIProgressDialog.INSTALL_DONE,
  168.                              getLocalizedError("error-261"));
  169.       result.remove(false);
  170.       return;
  171.     }
  172.  
  173.     this._fireNotification(nsIXPIProgressDialog.INSTALL_START);
  174.  
  175.     result.QueryInterface(Components.interfaces.nsILocalFile);
  176.     try {
  177.       result.launch();
  178.       this._fireNotification(nsIXPIProgressDialog.INSTALL_DONE, null);
  179.       // It would be nice to remove the tempfile, but we don't have
  180.       // any way to know when it will stop being used :-(
  181.     }
  182.     catch (e) {
  183.       Components.utils.reportError(e);
  184.       this._fireNotification(nsIXPIProgressDialog.INSTALL_DONE,
  185.                              getLocalizedError("error-207"));
  186.       result.remove(false);
  187.     }
  188.   },
  189.  
  190.   onProgress: function(aRequest, aContext, aProgress, aProgressMax)
  191.   {
  192.     gPluginInstaller.pluginInstallationProgressMeter(this._plugin.pid,
  193.                                                      aProgress,
  194.                                                      aProgressMax);
  195.   },
  196.  
  197.   onStatus: function(aRequest, aContext, aStatus, aStatusArg)
  198.   {
  199.     /* pass */
  200.   }
  201. };
  202.  
  203. var PluginInstallService = {
  204.  
  205.   /**
  206.    * Start installation of installers and XPI plugins.
  207.    * @param aInstallerPlugins An array of objects which should have the
  208.    *                          properties "pid", "InstallerLocation",
  209.    *                          and "InstallerHash"
  210.    * @param aXPIPlugins       An array of objects which should have the
  211.    *                          properties "pid", "XPILocation",
  212.    *                          and "XPIHash"
  213.    */
  214.   startPluginInstallation: function (aInstallerPlugins,
  215.                                      aXPIPlugins)
  216.   {
  217.     this._installerPlugins = [new InstallerObserver(plugin)
  218.                               for each (plugin in aInstallerPlugins)];
  219.     this._installersPending = this._installerPlugins.length;
  220.  
  221.     this._xpiPlugins = aXPIPlugins;
  222.  
  223.     if (this._xpiPlugins.length > 0) {
  224.       this._xpisDone = false;
  225.  
  226.       var xpiManager = Components.classes["@mozilla.org/xpinstall/install-manager;1"]
  227.                                  .createInstance(Components.interfaces.nsIXPInstallManager);
  228.       xpiManager.initManagerWithHashes(
  229.         [plugin.XPILocation for each (plugin in this._xpiPlugins)],
  230.         [plugin.XPIHash for each (plugin in this._xpiPlugins)],
  231.         this._xpiPlugins.length, this);
  232.     }
  233.     else {
  234.       this._xpisDone = true;
  235.     }
  236.   },
  237.  
  238.   _fireFinishedNotification: function()
  239.   {
  240.     if (this._installersPending == 0 && this._xpisDone)
  241.       gPluginInstaller.
  242.         pluginInstallationProgress(null, nsIXPIProgressDialog.DIALOG_CLOSE,
  243.                                    null);
  244.   },
  245.  
  246.   // XPI progress listener stuff
  247.   onStateChange: function (aIndex, aState, aValue)
  248.   {
  249.     // get the pid to return to the wizard
  250.     var pid = this._xpiPlugins[aIndex].pid;
  251.     var errorMsg;
  252.  
  253.     if (aState == nsIXPIProgressDialog.INSTALL_DONE) {
  254.       if (aValue != 0) {
  255.         var xpinstallStrings = document.getElementById("xpinstallStrings");
  256.         try {
  257.           errorMsg = xpinstallStrings.getString("error" + aValue);
  258.         }
  259.         catch (e) {
  260.           errorMsg = xpinstallStrings.getFormattedString("unknown.error", [aValue]);
  261.         }
  262.       }
  263.     }
  264.  
  265.     if (aState == nsIXPIProgressDialog.DIALOG_CLOSE) {
  266.       this._xpisDone = true;
  267.       this._fireFinishedNotification();
  268.     }
  269.     else {
  270.       gPluginInstaller.pluginInstallationProgress(pid, aState, errorMsg);
  271.     }
  272.   },
  273.  
  274.   onProgress: function (aIndex, aValue, aMaxValue)
  275.   {
  276.     // get the pid to return to the wizard
  277.     var pid = this._xpiPlugins[aIndex].pid;
  278.     gPluginInstaller.pluginInstallationProgressMeter(pid, aValue, aMaxValue);
  279.   }
  280. }
  281.